package cn.edu.nuc.zyb.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import cn.edu.nuc.zyb.model.Goods;

public class AdminClient {

	Socket socket;

	public AdminClient() {
		try {
			socket = new Socket("127.0.0.1", 4000);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean acceptMsg() {
		InputStream is = null;
		DataInputStream dis = null;
		try {
			is = socket.getInputStream();
			dis = new DataInputStream(is);
			return dis.readBoolean();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			try {
				is.close();
				dis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;

	}

	public void listGoods() {
		String msg = "adminlistgoods,";
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void searchGoods(int gid) {
		String msg = "adminsearchgoods," + gid;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public Goods acceptSearchGoods() {
		try {

			ObjectInputStream oos = new ObjectInputStream(socket.getInputStream());
			List<Goods> list = (List<Goods>) oos.readObject();
			System.out.println(list.size());
			return list.get(0);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}

	public void addGoods(String goodsName, float price, int num, String intro) {
		String msg = "addgoods," + goodsName + "," + price + "," + num + "," + intro;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void changeGoods(String Goodsname, float price, int num, String intro) {
		String msg = "changegoods," + Goodsname + "," + price + "," + num + "," + intro;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deleteGoods(String goodname) {

		String msg = "deletegoods," + goodname;
		OutputStream os = null;
		DataOutputStream dos = null;
		try {
			os = socket.getOutputStream();
			dos = new DataOutputStream(os);
			dos.writeUTF(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
